#ifndef grocerylistpanel_h
#define grocerylistpanel_h

#include "juce_amalgamated.h"
#include "GroceryListView.h"
#include "../ListPanel.h"
#include "../PersistentFoodList.h"
#include "../../FoodObjects/Food.h"
#include <cmath>
#include <time.h>

class GroceryListPanel: public ListPanel {
private:
	GroceryListView* glView;
	int currentItem;

public:
	GroceryListPanel(MasterComponent* master, int w, int h) :
		ListPanel(master, w, h), currentItem(0) {

		deque<Eatable>& items = PersistentFoodList::instance().getFoods();
		copy(items.begin(), items.end(), inserter(filteredItems, filteredItems.begin()));

		addAndMakeVisible(glView = new GroceryListView());
		glView->setBounds(35, 94, 5 * width / 7 - 70, height - 123);
		glView->setListPanel(this);

		srand(time(0));
		//		for(int i = 0; i  < 40; i++) {
		//			glView->addItem(filteredItems[rand() % filteredItems.size()]);
		//		}

		listBox->updateContent();
		backButton->setFromTo(MasterComponent::GROCERY_LIST, MasterComponent::MAIN);

	}

	void paint(Graphics& g) {
		Rectangle r(0, 0, width, height);
		g.setColour(Colours::palegoldenrod);
		g.fillRoundedRectangle(r, 50);
		g.setColour(Colours::grey);
		MiscGraphics::drawRoundedRect(g, r, 50, 1);

		g.setColour(Colours::black);
		g.setFont(Font(Font::getDefaultSansSerifFontName(), 36, Font::plain));
		g.drawFittedText(T("Grocery List"), 0, 0, width, 100, Justification::centred, 1);

		paintListBox(g);
	}

	void mouseDown(const MouseEvent& e) {

	}

	void switchPanels(int from, int to) {
		master->switchPanels(from, to);
	}

	void showItemView(int row) {
		currentItem = row;
	}

//	void filterList(String searchString) {
//		filteredItems.clear();
//		String name;
//		if (searchString.isEmpty()) {
//			vector<Eatable>& foods = FoodList::instance().getFoods();
//			copy(foods.begin(), foods.end(), inserter(filteredItems, filteredItems.begin()));
//		} else {
//			for (vector<Eatable>::iterator eit = FoodList::instance().getFoods().begin(); eit
//					!= FoodList::instance().getFoods().end(); ++eit) {
//				name = eit->getName();
//				if (name.containsIgnoreCase(searchString)) {
//					filteredItems.push_back(*eit);
//				}
//			}
//		}
//
//		listBox->updateContent();
//	}

	void doubleClicked(const MouseEvent& e) {
		glView->addItem(filteredItems[currentItem]);
	}

	void addCurrentItem() {
		glView->addItem(filteredItems[currentItem]);
	}

	deque<Eatable>& getEatableList() {
		return PersistentFoodList::instance().getFoods();
	}

	GroceryListView* getView() {
		return glView;
	}
};

#endif
